<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/2/28
 * Time: 18:53
 */

namespace SwoCloud\Server;;

use Swoole\Server as SwooleServer;

use SwoCloud\Server\Traits\AckTraits;
/**
 * 所有服务的父类， 写一写公共的操作
 */
abstract class Server
{
    use AckTraits;
    //属性
    protected $swooleServer;

    protected $port=9500;

    protected $host="0.0.0.0";

    protected $watchFile=false;

    /**
     * 这是swoole服务的配置
     * @var [type]
     */
    protected $config=[
        'task_worker_num' => 0,
    ];



    //自定义回调函数
    protected $event=[
        'server' => [
            //事件  以及事件函数
            "start"        => "onStart",
            "managerStart" => "onManagerStart",
            "managerStop"  => "onManagerStop",
            "shutdown"     => "onShutdown",
            "workerStart"  => "onWorkerStart",
            "workerStop"   => "onWorkerStop",
            "workerError"  => "onWorkerError",
        ],
        'sub' => [

        ],
        'ext'=>[]
    ];

    public function __construct()
    {

        //1.创建
        $this->createServer();

        //3.设置需要注册的回调函数--绑定回调函数
        $this->initEvent();
        //4.设置swoole的回调函数 --调用回调函数
        $this->setSwooleEvent();


    }

    //通用方法
    public function start()
    {
        $this->createTable();

        // 2. 设置配置信息
        $this->swooleServer->set($this->config);
        // 5. 启动
        $this->swooleServer->start();

    }


    /**
     * 创建服务
     * 六星教育 @shineyork老师
     */
    protected abstract function createServer();
    /**
     * 初始化监听的事件
     * 六星教育 @shineyork老师
     */
    protected abstract function initEvent();

    /**
     * 设置swoole的回调事件
     * 六星教育 @shineyork老师
     */
    protected function setSwooleEvent()
    {
        foreach ($this->event as $type => $events) {
            foreach ($events as $event => $func) {
                $this->swooleServer->on($event, [$this, $func]);
            }
        }
    }


    //回调方法

    public function onStart(SwooleServer $server)
    {

    }

    public function onManagerStart(SwooleServer $server)
    {

    }
    public function onManagerStop(SwooleServer $server)
    {

    }
    public function onShutdown(SwooleServer $server)
    {

    }
    public function onWorkerStart(SwooleServer $server, int $worker_id)
    {

    }



    public function onWorkerStop(SwooleServer $server, int $worker_id)
    {

    }
    public function onWorkerError(SwooleServer $server, int $workerId, int $workerPid, int $exitCode, int $signal)
    {

    }

    public function setEvent($type, $event)
    {
        // 暂时不支持直接设置系统的回调事件
        if ($type == "server") {
            return $this;
        }
        $this->event[$type] = $event;
        return $this;
    }

    /**
     * @return array
     */
    public function getConfig(): array
    {
        return $this->config;
    }

    /**
     * @param array $config
     *
     * @return static
     */
    public function setConfig($config)
    {
        $this->config = array_map($this->config, $config);
        return $this;
    }

    public function setWatchFile($watchFile){
        $this->watchFile=$watchFile;
    }

    public function getHost()
    {
        return $this->host;
    }

    public function getPort()
    {
        return $this->port;
    }
}